#include<iostream>
#include<vector>
using namespace std;



class Solution
{
    // 扔进去AC了他
public:
    vector<int> spiralOrder(vector< vector<int> > & matrix)
    {

    int n = matrix.size();
    int m = matrix[0].size();
    vector<int> arr2;
    //cout<<n<<" "<<m<<endl;
    int x1=0;
    int y1=0;
    int x2= m-1;
    int y2 = n-1;
//    cout<<"x "<<x1 <<" "<<x2 <<" "<< y1<<" "<<y2<<endl;
    int k=0;
    while( x1<=x2 && y1<=y2)
    {
        for(int i=x1;i<=x2;i++)
            arr2.push_back(matrix[y1][i]);
        for(int i=y1+1;i<=y2;i++)
            arr2.push_back(matrix[i][x2]);

        if(x1<x2 && y1<y2)
        {for(int i=x2-1;i>=x1;i--)
           arr2.push_back(matrix[y2][i]);
        for(int i=y2-1;i>y1;i--)
           arr2.push_back(matrix[i][x1]);
        }
        x1+=1;
        y1+=1;
        x2-=1;
        y2-=1;
    }


//   for(int i=0;i<n*4;i++)
//        cout<<arr2[i]<<" ";
//
//    cout<<endl;


    return arr2;
    }


};




int main() {
    int a[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
//    int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};

//    int a[4][4] =  {{1,2,3,4],[5,6,7,8],[9,10,11,12]]
    int n = sizeof(a[0])/sizeof(a[0][0]);
    vector< vector<int> > arr;
    for(int i =0;i<3;i++)
    {
        vector<int> arr1(a[i],a[i]+n);
        arr.push_back(arr1);
    }
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<3;j++)

        {
            cout<<a[i][j] << " ";
        }
         cout<<endl;
    }


    vector<int> arr2(n*3);



    arr2 = Solution().spiralOrder(arr);

    for(int i=0;i<n*3;i++)
        cout<<arr2[i]<<" ";

    cout<<endl;
    return 0;
}